import db from './createDb'
import Mock from 'mockjs'

// 读取轮播图片列表信息
Mock.mock('carousel/list', 'get', () => {
  const result = db.select('zy_carousel')
  return { err: 0, result, desc: '查询成功' }
})

// 添加轮播图片
Mock.mock('carousel/add', 'post', ({ body }) => {
  const { title, picture, picurl, orderid, state = 1 } = JSON.parse(body)
  db.add('zy_carousel', { title, picture, picurl, orderid, state })
  return {
    err: 0,
    desc: '添加成功'
  }
})

// 修改轮播图片
Mock.mock('carousel/updata', 'post', ({ body }) => {
  const { id, title, picture, picurl, orderid, state } = JSON.parse(body)
  db.put('zy_carousel', id, { title, picture, picurl, orderid, state })
  return {
    err: 0,
    desc: '修改成功'
  }
})

// 修改state状态
Mock.mock('carousel/state', 'post', ({ body }) => {
  const { id, state } = JSON.parse(body)
  db.put('zy_carousel', id, { state })
  return {
    err: 0,
    desc: '状态修改成功'
  }
})

// 删除轮播图片
Mock.mock('carousel/delete', 'post', ({ body }) => {
  const { ids } = JSON.parse(body)
  db.delete('zy_carousel', ids)
  return {
    err: 0,
    desc: '删除成功'
  }
})
